import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React, { PureComponent } from 'react';
import { object, func } from 'prop-types';
import { connect } from 'react-redux';

import Modal from 'antd/lib/modal';

import * as actions from './modules/system';
import ErrorModal from './components/ErrorModal';
import { identity } from './libs/utils';

export var Root = function (_PureComponent) {
  _inherits(Root, _PureComponent);

  function Root() {
    _classCallCheck(this, Root);

    return _possibleConstructorReturn(this, _PureComponent.apply(this, arguments));
  }

  Root.prototype.componentDidMount = function componentDidMount() {
    document.body.classList.add('ProximaNova--loaded');
  };

  Root.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var system = nextProps.system;
    var errorAlert = system.errorAlert;

    var originalErrorAlert = this.props.system.errorAlert;

    if (originalErrorAlert !== errorAlert) {
      if (this.currentErrorAlert) {
        this.currentErrorAlert.destroy();
        this.currentErrorAlert = null;
      }

      if (errorAlert) {
        this.currentErrorAlert = Modal.error({
          content: errorAlert,
          onCancel: this.props.errorAlertHide
        });
        this.props.errorAlert(errorAlert);
      }
    }
  };

  Root.prototype.render = function render() {
    var _props = this.props,
        store = _props.store,
        view = _props.view,
        system = _props.system,
        errorModalHide = _props.errorModalHide,
        modalHide = _props.modalHide;
    var errorModal = system.errorModal,
        modal = system.modal;


    var ModalView = void 0;
    var ModalViewData = void 0;
    if (modal) {
      ModalView = modal.view;
      ModalViewData = modal.data;
    }

    return React.createElement(
      'div',
      null,
      view(store),
      errorModal && React.createElement(ErrorModal, { error: errorModal, onClose: errorModalHide }),
      modal && React.createElement(ModalView, _extends({ onClose: modalHide }, ModalViewData))
    );
  };

  return Root;
}(PureComponent);

Root.propTypes = {
  system: object, // eslint-disable-line
  store: object, // eslint-disable-line
  view: func,
  errorModalHide: func,
  errorAlert: func,
  errorAlertHide: func,
  modalHide: func
};
Root.defaultProps = {
  system: {},
  view: identity
};
var mapStateToProps = function mapStateToProps(state) {
  return {
    system: state.system
  };
};

export default connect(mapStateToProps, actions)(Root);